package cn.maidouya.org.controller;

import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.org.domain.Employee;
import cn.maidouya.org.query.EmployeeQuery;
import cn.maidouya.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     *   批量删除
     */
    @PatchMapping
    @ApiOperation(value = "批量删除员工",notes = "批量删除员工")
    public AjaxResult batchDel(@RequestBody Long[] ids) {
        System.err.println(Arrays.toString(ids));
        try {
        employeeService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
    }
    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找员工",notes = "根据ID查找员工")
    public Employee loadById(@PathVariable Long id) {
        Employee one = employeeService.loadById(id);
        return one;
    }
    @GetMapping
    @ApiOperation(value = "查找全部员工",notes = "查找所有员工信息")
    public List<Employee> loadAll() {
        List<Employee> list = employeeService.loadAll();
        return list;
    }
    @PostMapping
    @ApiOperation(value = "查找员工分页数据",notes = "分页查找所有员工信息")
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery) {
        PageList<Employee> pageList = employeeService.queryData(employeeQuery);
        return pageList;
    }
    @PutMapping
    @ApiOperation(value = "添加和修改员工",notes = "添加和修改员工")
    public AjaxResult saveAndUpdate(@RequestBody Employee employee) {
                try {
                    if (employee.getId() == null){
                        employeeService.save(employee);
                    } else {
                        employeeService.update(employee);
                    }
                    return new AjaxResult();
                } catch (Exception e) {
                    e.printStackTrace();
                    return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
                }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工",notes = "根据id删除员工")
    public AjaxResult delete(@PathVariable Long id) {
        try {
          employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }
}